package com.group.itms.routes.activities;

import java.sql.SQLException;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.group.itms.routes.R;
import com.group.itms.routes.data.BObject;
import com.group.itms.routes.data.BObjectOption;
import com.group.itms.routes.data.Option;

public class InformationPointActivity extends BaseActivity {

	private static final String TAG = "InformationPointActivity";
	private static final String FROM_MAP = "from_map";
	
	private TextView mLocation;
	private TextView mTarget;
	private TextView mDescription;

	private BObject mInfoPoint;
	private boolean mFromMap;

	public static Intent getIntent(Context c, int objId) {
		return getIntent(c, objId, false);
	}
	
	public static Intent getIntent(Context c, int objId, boolean fromMap) {
		Intent intent = new Intent(c, InformationPointActivity.class);
		intent.putExtra(OBJECT_ID, objId);
		intent.putExtra(FROM_MAP, fromMap);
		return intent;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_infromation_point);
		
		mFromMap = getIntent().getBooleanExtra(FROM_MAP, false);

		mLocation = (TextView) findViewById(R.id.tvLocation);
		mTarget = (TextView) findViewById(R.id.tvTarget);
		mDescription = (TextView) findViewById(R.id.tvDescription);

		init();

		BObjectOption loc = getLocationText(mInfoPoint);
		BObjectOption goal = getGoalText(mInfoPoint);
		BObjectOption genDec = getGeneralDesText(mInfoPoint);

		if (loc != null)
			mLocation.setText(loc.getText());
		if (goal != null)
			mTarget.setText(goal.getText());
		if (genDec != null)
			mDescription.setText(genDec.getText());
	}

	private void init() {
		Bundle args = getIntent().getExtras();
		if (args == null) {
			throw new RuntimeException(
					"InfoDetailActivity does not have aruments");
		}
		int mInfoPointId = args.getInt(OBJECT_ID, -1);

		if (mInfoPointId == -1) {
			throw new RuntimeException("Missing args with key: " + OBJECT_ID
					+ " to activity: " + getClass());
		}
		mInfoPoint = getInfoPint(mInfoPointId);

		if (mInfoPoint == null) {
			throw new RuntimeException("Unable to find Object with id: "
					+ mInfoPointId);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (!mFromMap) {
			getSupportMenuInflater().inflate(R.menu.menu_show_on_map, menu);
		}
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
			switch (item.getItemId()) {
			case R.id.show_on_map:
				startActivity(NewMapActivity.getIntent(this, mInfoPoint.getId(), true));
				break;
			}
		return super.onOptionsItemSelected(item);
	}
	
	private BObjectOption getLocationText(BObject obj) {

		try {
			return getHelper().getObjectOptionDao().queryBuilder().where()
					.eq(BObjectOption.IS_ACTIVE, true)
					.and()
					.eq(BObjectOption.OPTION_COLUMN_NAME, getLocation()).and()
					.eq(BObjectOption.BOBJECT_COLUMN_NAME, obj).queryForFirst();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	private BObjectOption getGoalText(BObject obj) {
		try {
			return getHelper().getObjectOptionDao().queryBuilder().where()
					.eq(BObjectOption.IS_ACTIVE, true)
					.and()
					.eq(BObjectOption.OPTION_COLUMN_NAME, getGoal()).and()
					.eq(BObjectOption.BOBJECT_COLUMN_NAME, obj).queryForFirst();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	private BObjectOption getGeneralDesText(BObject obj) {
		try {
			return getHelper()
					.getObjectOptionDao()
					.queryBuilder()
					.where()
					.eq(BObjectOption.IS_ACTIVE, true)
					.and()
					.eq(BObjectOption.OPTION_COLUMN_NAME, getGeneralDescription())
					.and()
					.eq(BObjectOption.BOBJECT_COLUMN_NAME, obj).queryForFirst();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	private Option getLocation() {
		try {
			return getHelper().getOptionDao().queryBuilder().where()
					.eq(Option.IS_ACTIVE, true)
					.and()
					.eq(Option.NAME, "Location").queryForFirst();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	private Option getGoal() {
		try {
			return getHelper().getOptionDao().queryBuilder().where()
					.eq(Option.IS_ACTIVE, true)
					.and()
					.eq(Option.NAME, "Goal").queryForFirst();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	private Option getGeneralDescription() {
		try {
			return getHelper().getOptionDao().queryBuilder().where()
					.eq(Option.IS_ACTIVE, true)
					.and()
					.eq(Option.NAME, "General descritption").queryForFirst();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	private BObject getInfoPint(int id) {

		try {
			return getHelper().getObjectDao().queryForId(id);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	protected String getTag() {
		return TAG;
	}
}
